package com.jinghua.treatment.controller;

import com.jinghua.treatment.common.GlobalConstant;
import com.jinghua.treatment.common.controller.BaseController;
import com.jinghua.treatment.common.domain.JSONResult;
import com.jinghua.treatment.entity.SysInquiryAnswers;
import com.jinghua.treatment.entity.SysInquirySubjectCategory;
import com.jinghua.treatment.entity.SysInquiryTemplate;
import com.jinghua.treatment.service.SysInquiryAnswersService;
import com.jinghua.treatment.service.SysInquiryService;
import com.jinghua.treatment.service.SysInquirySubjectCategoryService;
import com.jinghua.treatment.service.SysInquiryTemplateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问诊单模板控制器
 * Created by 王兴光 on 2018/5/27.
 */
@Controller
@RequestMapping("/template")
public class SysInquiryTemplateController extends BaseController {

    /******************************************   初始化模板 start  *************************************************/
    private Map<Integer, List> templates = new HashMap();
    private  List<SysInquirySubjectCategory> findTemplateDataById(Integer templateId){
        List<SysInquirySubjectCategory> templateList = templates.get(templateId);
        if(templateList==null){
            templateList =  inquiryTemplateService.selectTemplateList(templateId);
        }
        return templateList;
    }
    @PostConstruct
    private void initTemplate(){
        //获取所有模板编号
        List<SysInquiryTemplate> sysInquiryTemplateList = inquiryTemplateService.selectTemplateList();
        if(sysInquiryTemplateList!=null&&sysInquiryTemplateList.size()>0){
            for(SysInquiryTemplate sysInquiryTemplate:sysInquiryTemplateList){
                List<SysInquirySubjectCategory>  tempList =  inquiryTemplateService.selectTemplateList(sysInquiryTemplate.getUuid());
                templates.put(sysInquiryTemplate.getUuid(),tempList);
            }
            logger.debug("问诊单模板数据已初始化完毕");
        }
    }
    /******************************************   初始化模板 end *************************************************/



    // 大分类业务对象
    @Resource
    private SysInquiryTemplateService inquiryTemplateService;

    @ApiOperation(value = "根据模板编号，获取问诊单一级分类列表")
    @RequestMapping(value = "/getListByTemplate")
    @ResponseBody
    public JSONResult getSubjectByTemplate(Integer templateId){
       /* List<SysInquirySubjectCategory> templateList = null;
        if(super.session.getAttribute("templateList")==null) {
            templateList = inquiryTemplateService.selectTemplateList(templateId);
            super.session.setAttribute("templateList",templateList);
        }else{
            templateList = (List<SysInquirySubjectCategory>)super.session.getAttribute("templateList");
        }*/

        List<SysInquirySubjectCategory> templateList =findTemplateDataById(templateId);
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取问诊单模板",templateList);
    }

    @Resource
    private SysInquiryAnswersService inquiryAnswersService;
    @Resource
    private SysInquirySubjectCategoryService inquirySubjectCategoryService;
    @Resource
    private SysInquiryService inquiryService;

    @ApiOperation(value = "根据问诊单编号获取问诊单详情信息")
    @RequestMapping(value = "/getListByInquiryId",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getListByInquiryId(String inquiryId){
        System.out.println("inquiryId："+inquiryId);
        SysInquiryAnswers inquiry = inquiryAnswersService.selectByPrimaryKey(Integer.valueOf(inquiryId));
        List<SysInquirySubjectCategory> templateDetailList = findTemplateDataById(inquiryService.selectById(inquiry.getInquiryId()).getTemplateId());

        inquiry.setDetailList(templateDetailList);  // 添加问诊单模板列表

        inquiry.setSubList(inquirySubjectCategoryService.selectSubListByInquiryId(inquiryId));
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"查询问诊单信息成功",inquiry);
    }

    @ApiOperation(value = "获取模板信息列表")
    @RequestMapping(value = "/getTemplateList")
    @ResponseBody
    public JSONResult getTemplateList(){
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取问诊单模板列表",inquiryTemplateService.selectTemplateList());
    }

}
